import { Component, Input, NgModule, OnInit, ViewChild } from '@angular/core';
import { HttpService } from "../../../http/http.service";
import { NotifyService } from "../../../utils/notify.service";
import { NzModalRef } from "ng-zorro-antd";
import { MetaTableComponent } from "../../meta/meta-table/meta-table.component";
import { CommonModule } from "@angular/common";
import { Tool } from "../../../utils/tool";


@NgModule({
  imports: [
    CommonModule
  ]
})
@Component({
  selector: 'app-defalut-dialog',
  templateUrl: './defalut-dialog.component.html',
  styleUrls: ['./defalut-dialog.component.styl']
})
export class DefalutDialogComponent implements OnInit {

  constructor(private http: HttpService, private notify: NotifyService, private modalRef: NzModalRef) {
  }

  @ViewChild('mainTable', { static: true }) mainTable: MetaTableComponent;
  @Input() params = { btnId: '', rowData: { id: '' } };
  @Input() parentPage;
  citycount;
  provincecount;
  industrycount;
  treeData = [];

  ngOnInit() {
    this.getTempList();
    this.getIndustryCount();
    this.getCityCount();
    this.getProvinceCount();
  }

  onSearch() {
    this.formateTime();
    this.mainTable.onSearch();
    this.getTempList();
    this.getIndustryCount();
    this.getCityCount();
    this.getProvinceCount();
  }

  searchForm = {
    startDate: '',
    endDate: '',
  };


  columns = [
    {
      title: '行业名称',
      item: 'name',
      width: 120
    }];

  //获取测评模块名称
  getTempList() {
    let para = {
      page: 0,
      limit: 0,
      params: encodeURI(JSON.stringify({
        startDate: this.searchForm.startDate,
        endDate: this.searchForm.endDate,
      }))
    };
    this.http.get('/evl/company/provincecity', para).subscribe(res => {
      if (res.err == '0') {
        this.treeData = res.data;
      } else {
        this.notify.error(res.errMsg).subscribe();
      }
    });
  }

  getProvinceCount() {
    let para = {
      params: encodeURI(JSON.stringify({
        startDate: this.searchForm.startDate,
        endDate: this.searchForm.endDate,
      }))
    }
    this.http.get("evl/company/provincecountformap", para).subscribe(res => {
      if (res.err == '0') {
        this.provincecount = res.data;
      } else {
        this.notify.error(res.errMsg).subscribe();
      }
    });
  }

  getCityCount() {
    let para = {
      params: encodeURI(JSON.stringify({
        startDate: this.searchForm.startDate,
        endDate: this.searchForm.endDate,
      }))
    }
    this.http.get("evl/company/citycountformap", para).subscribe(res => {
      if (res.err == '0') {
        this.citycount = res.data;
      } else {
        this.notify.error(res.errMsg).subscribe();
      }
    });
  }

  getIndustryCount() {
    let para = {
      params: encodeURI(JSON.stringify({
        startDate: this.searchForm.startDate,
        endDate: this.searchForm.endDate,
      }))
    }

    this.http.get("evl/industry/industrycountformap", para).subscribe(res => {
      if (res.err == '0') {
        this.industrycount = res.data;
      } else {
        this.notify.error(res.errMsg).subscribe();
      }
    });
  }

  onCancel() {
    this.modalRef.destroy();
  }


  // 不可选择的开始日期
  disabledStartDate = (startValue: Date): boolean => {
    if (!startValue || !this.searchForm.endDate) {
      return false;
    }
    return Tool.dateFormat('YYYY-mm-dd', startValue) > Tool.dateFormat('YYYY-mm-dd', this.searchForm.endDate);
  };

  // 不可选择的结束日期
  disabledEndDate = (endValue: Date): boolean => {
    if (!endValue || !this.searchForm.startDate) {
      return false;
    }
    return Tool.dateFormat('YYYY-mm-dd', endValue) < Tool.dateFormat('YYYY-mm-dd', this.searchForm.startDate);
  };

  // 时间格式化，清空时分秒
  formateTime() {
    if (this.searchForm.startDate == null) {
      this.searchForm.startDate = '';
    }
    if (this.searchForm.endDate == null) {
      this.searchForm.endDate = '';
    }
    // 创建时间
    if (this.searchForm.startDate != null && this.searchForm.startDate != "") {
      this.searchForm.startDate = Tool.dateFormat('YYYY-mm-dd', this.searchForm.startDate);
    }
    // 预计结束时间
    if (this.searchForm.endDate != null && this.searchForm.endDate != "") {
      this.searchForm.endDate = Tool.dateFormat('YYYY-mm-dd', this.searchForm.endDate);
    }
  }
}


